<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 部门 控制层
 * @author adela.xu 8.29
 */
class permis extends My_Controller
{
    private $assign = array();
    /**
     * 默认方法
     * @author Toby.tu 2016-09-01
     */
    public function init(){
        $this->load->model('Permis_model','permis_model');
    }
    /**
     * 用户列表
     * @author Toby.tu 2016-08-30
     */
    public function index(){
        $permislist = $this->permis_model->getAll();
        $this->assign['permislist'] = $permislist;
        $this->assign['view'] = 'admin/permis/index';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 创建页面
     * @author Toby.tu 2016-08-31
     */
    public function create(){
        $this->assign['view'] = 'admin/permis/create';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 新增权限组
     * @author Toby.tu 2016-08-31
     */
    public function store(){
        try {
            $input = $this->input->post();
            if(!isset($input['permis'])){
                throw new Exception('参数不正确。');
            }
            if(empty($input['permis']['name'])){
                throw new Exception('请输入权限组名称。');
            }
            $permis = $this->permis_model->getOne(['name'=>$input['permis']['name']]);
            if(!empty($permis)){
                throw new Exception('权限组已存在。');
            }
            $param = $input['permis'];
            $param['create_time'] = $this->phpdate->time();
            $param['update_time'] = $param['create_time'];
            $this->permis_model->newData($param);
            redirect('admin/permis/index');die;
        } catch (Exception $e) {
            $this->assign += $input['permis'];
            $this->assign['error_code'] = $e->getMessage();
            $this->assign['view'] = 'admin/permis/create';
            $this->load->view('admin/common/frame',$this->assign);
        }
    }
    /**
     * 创建页面
     * @author Toby.tu 2016-08-31
     */
    public function edit($id){
        $menu = $this->permis_model->getOne(['id'=>$id]);
        unset($menu['status']);
        $this->assign += $menu;
        $this->assign['id'] = $id;
        $this->assign['view'] = 'admin/permis/edit';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 新增权限组
     * @author Toby.tu 2016-08-31
     */
    public function update($id){
        try {
            $input = $this->input->post();
            if(!isset($input['permis'])){
                throw new Exception('参数不正确。');
            }
            if(empty($input['permis']['name'])){
                throw new Exception('请输入权限组名称。');
            }
            $permis = $this->permis_model->getOne(['name'=>$input['permis']['name']]);
            if(!empty($permis) && $id != $permis['id']){
                throw new Exception('权限组已存在。');
            }
            $param = $input['permis'];
            $param['update_time'] = $this->phpdate->time();
            $this->permis_model->updateById($id,$param);
            redirect('admin/permis/index');die;
        } catch (Exception $e) {
            $this->assign += $input['permis'];
            $this->assign['id'] = $id;
            $this->assign['error_code'] = $e->getMessage();
            $this->assign['view'] = 'admin/permis/edit';
            $this->load->view('admin/common/frame',$this->assign);
        }
    }
    /**
     * 禁用启用
     * @author Toby.tu 2016-08-31
     */
    public function enable($id){
        try {
            $input = $this->input->get();
            if(!isset($input['status'])){
                throw new Exception('参数不正确。');
            }
            $param = array();
            $param['status'] = ($input['status']==1)?0:1;
            $this->permis_model->updateById($id,$param);
            redirect('admin/permis/index');die;
        } catch (Exception $e) {
            redirect('admin/permis/index');die;
        }
    }
    /**
     * 删除
     * @author Toby.tu 2016-08-31
     */
    public function destroy($id){
        $this->user_model->deleteById($id);
        redirect('admin/User/index');die;
    }
    /**
     * 设置权限
     * @author Toby.tu 2017-01-05
     */
    public function setpermis($id){
        $this->load->model('Menu_model','menu_model');
        $this->load->model('Permismodule_model','permismodule_model');
        $this->vendor('logic/PermissionLogic','permis_logic');
        $permis_module_list = $this->menu_model->getAll();
        $permis_module_list = $this->permis_logic->getPermisModuleList($permis_module_list);
        $permis_list = $this->permismodule_model->getPermisModuleByGroupId($id);
        $this->assign['permis_module_list'] = $permis_module_list;
        $this->assign['permis_list'] = $permis_list;
        $this->assign['permis_id'] = $id;
        $this->assign['view'] = 'admin/permis/permis';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 设置权限
     * @author Toby.tu 2017-01-05
     */
    public function permismodule($id){
        try {
            $input = $this->input->post();
            if(!$input['permis_module']){
                throw new Exception('参数不正确。');
            }
            $this->load->model('Permismodule_model','permismodule_model');
            $permis_module_id = $input['permis_module_id'];
            $permis_module = $input['permis_module'];
            //先删除
            $this->permismodule_model->deletePermisByGroupId($id);
            //再插入
            $param = $this->permis_logic->setPermisList($id,
                        $permis_module_id,$permis_module);
            if(!empty($param)){
                $this->permismodule_model->newManyData($param);
            }
            redirect('admin/permis/index');die;
        } catch (Exception $e) {

        }
    }
}
